import 'package:ls_work/a_elder/models/evaluation_entity.dart';
import 'package:ls_work/mvp/base_page_presenter.dart';
import 'package:ls_work/net/net.dart';
import 'package:ls_work/a_elder/page/elder_evaluate_report.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/widgets/state_layout.dart';
import 'dart:convert';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:dio/dio.dart';

class EvaluatePresenter
    extends BasePagePresenter<ElderEvaluateReportPageState> {
  Future fectEvaluateData(int? customerId, int page, bool isShowDialog) async {
    view.showProgress();
    var map = {'customerId': customerId, "page": page, "rows": 20};
    HttpUtil.instance
        .getDio()
        .post(HttpApi.evaluatioenRecordList, data: map,options: PostRequest.urlencodedHeader)
        .then((r) {
      view.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            EvaluateEntity model = EvaluateEntity.fromJson(map['data']);
            if ((model.rows??[]).length > 0) {
              view.provider.setHasMore((model.rows??[]).length == 20);
              if (page == 1) {
                /// 刷新
                view.provider.list.clear();
                view.provider.addAll(model.rows??[]);
              } else {
                view.provider.addAll(model.rows??[]);
              }
            } else {
              view.provider.setHasMore(false);
              view.provider.setStateType(StateType.empty);
            }
          } else {
            view.showToast(map["msg"]);
          }
        }
      } else {
        view.showToast(r.statusCode.toString());
        view.provider.setHasMore(false);
        view.provider.setStateType(StateType.empty);
      }
    }).catchError((e) {
      view.closeProgress();
      print(e.toString());
      view.provider.setHasMore(false);
      view.provider.setStateType(StateType.empty);
      view.showToast(getErrorMessage((e as DioError).type));
    });
  }
}
